package org.openfans.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openfans.domain.Auth;
import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class ActivateController extends AbstractController {
	private Facade facade = null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Auth auth = facade.getAuthByActivateCode(request.getParameter("code"));
		if (null != auth) {
			Fans fans = new Fans();
			fans.setEmail(auth.getEmail());
			fans.setNickName(auth.getName());
			fans.setJoinDate(new Date());
			fans.setIcon("images/f/default.jpg");
			facade.save(fans);
			auth.setFans(fans);
			facade.save(auth);
		}
		return new ModelAndView("redirect:main.html");
	}

}
